{extend name="public/container"}
{block name="content"}
<div id="myApp">
    <div class="douxue-filter padding-10 mb-20 flex flex-between" style="margin-bottom:20px">
        <el-form :inline="true" :model="where" size="mini" class="flex">
                <div class="flex flex-between">
                    <el-form-item>
                        <el-input size="small" v-model="where.name" placeholder="{:lang('User Level')}名称">
                            <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                        </el-input>
                    </el-form-item>
                </div>
        </el-form>

        <!-- el-button type="primary" size="mini" @click="doAdd()">新增</el-button -->
    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            v-loading="loading"
            element-loading-text="加载中"
            element-loading-spinner="el-icon-loading"
            size="mini">
            <el-table-column
                prop="id"
                label="ID"
                width="60"
                align="left">
            </el-table-column>
            <el-table-column
                prop="grade_type_display"
                label="等级类型"
                align="left">
            </el-table-column>
            <el-table-column
                prop="price"
                label="等级售价"
                align="left">
            </el-table-column>
            <el-table-column
                prop="expire_time"
                label="有效期"
                align="left">
                <template slot-scope="scope">
                    <el-link :underline="false" type="default" size="mini" v-if="scope.row.expire_type==2">获得后{{ scope.row.days }}内有效</el-link>
                    <el-link :underline="false" size="mini" v-else>{{ scope.row.expire_type_display }}</el-link>
                </template>
            </el-table-column>

            <el-table-column
                label="状态"
                align="left">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-color="#ff6a6c"
                        inactive-color="#999"
                        :active-value="1"
                        :inactive-value="0"
                        @change="doSetStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                width="140"
                align="left">
                <template slot-scope="scope">
                    <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                hide-on-single-page
                small
                layout="prev, pager, next"
                :total="rowsCount"
				:page-size="where.limit"
                @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>

    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" :visible.sync="dialogFormVisible" v-if="dialogFormVisible" @close='closeDialog' :close-on-click-modal="false">
        <el-form :model="formData" :rules="rules" ref="formData" label-width="80px" >
            <el-form-item label="等级类型" prop="grade_type">
                <el-select placeholder="效期类型" v-model="formData.grade_type" :clearable=true>
                    <el-option v-for="(item,key) in grade_types" :label="item.val" :key="key" :value="item.key"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="等级售价" prop="expire_time">
                <el-input v-model="formData.price" placeholder="等级售价" autocomplete="off" type="number">
                    <el-button slot="append">元</el-button>
                </el-input>
            </el-form-item>
            <el-form-item label="效期类型" prop="expire_type">
                <el-select placeholder="效期类型" v-model="formData.expire_type" :clearable=true>
                    <el-option v-for="(item,key) in expire_type" :label="item.name" :key="item.id" :value="item.id"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="有效天数" prop="expire_time" v-show="formData.expire_type==2">
                <el-input v-model="formData.days" placeholder="有效天数" autocomplete="off">
                    <el-button slot="append">天</el-button>
                </el-input>
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-radio v-model="formData.status" name="status" :label="1">开启</el-radio>
                <el-radio v-model="formData.status" name="status" :label="0">禁用</el-radio>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSave('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var my = new Vue({
        el: "#myApp",
        data(){
            return {
                where:{
                    cate_id:'',
                    name:'',
                    page:1,
                    limit:pageSize
                },
                dialogFormVisible: false,
                formData: {
                    id: '',
                    grade_type: 1,
                    status:1,
                    expire_type: 1,
                    days: 0,
                    price: 0
                },
				grade_types: [],
                rules: {
                    grade_type: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ]
                },
                loading:false,
                rowsCount: 0,
                rows: [],
                expire_type:[
                    {
                        id:2,
                        name:'固定天数'
                    }
                ]
            }
        },
        methods: {
            doLoadRows (page) {
                this.loading = true
                this.where.page  = page || 1
                axios.get(Url({a:'get_list' , q:this.where}))
                    .then(res => {
                        this.rows = res.data.data
                        this.rowsCount =res.data.count
                        this.loading = false
                    })
            },
            doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}}))
                    .then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                            onClose: _ => {
                                if (res.data.code !== 200) {
                                    this.doLoadRows()
                                }
                            }
                        })
                    })
            },
            doAdd (item) {
                if ( item != undefined ) {
                    this.formData = item;
                } else {
                    this.formData = {
                        id: '',
                        grade_type: 1,
                        status:1,
                        discount: 0,
                        expire_type: 1,
                        days: 0,
                    }
                }
                this.dialogFormVisible = true;
            },
            // 关闭
            closeDialog () {
                this.$refs['formData'].clearValidate();
            },
            onSave (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                this.$message({
                                    message:'操作成功',
                                    type:'success',
                                    duration:1500
                                });
                                this.dialogFormVisible = false;
                                this.doLoadRows(1)
                            }).catch(_ => {
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            })
                        })
                    } else {
                        return false;
                    }
                });
            },
            doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            },
			doLoadGradeTypes() {
				axios.get(Url({a:'get_attr' , q:{name:'grade_type'}})).then(res => {
				    this.grade_types = res.data.data
				}).catch(_ => {
				    this.$message.error('发生错误')
				})
			}
        },
        mounted(){
            this.doLoadRows()
			this.doLoadGradeTypes()
        }
    })
</script>
{/block}